Variables
This section will cover Python Variables.
Table of Contents
- Overview
- Names
- Camel Case
- Pascal Case
- Snake Case
- Assigning Values to Multiple Variables
- One Value to Multiple Variables
- Unpack a Collection
- Output Variables
- Global Variables
Overview
A variable in Python can have a short, descriptive, or long name. The name depends on the developer. However, there are a few rules to keep in mind when creating variables in Python.
The rules:
- The name must start with a letter or the underscore (
_) character. - A variable name cannot start with a number.
- A variable name can only contain alpha-numeric characters and underscores.
- Variable names are case-sensitive (name, Name, and NAME are three different variables).
- A variable name cannot be any of the Python keywords.
Names
The below are some examples of legal variable names.
myvar = "var1"
my_var = 'var2'
_my_var = "var 3"
myVar = "legal 1"
MyVar = "legal 2"
MYVAR = "legal 3"
myvar7 = "var4"
Examples of illegal names are:
my var = "var with space"
my-var = "var with dash"
1myvar = "var that starts with a number"
For easier readability, the coder can use different techniques to make it more readable.
Camel Case
Each word except the first, starts with a capital letter.
thisIsMyVar = "Test"
Pascal Case
Each word starts with a capital letter.
ThisIsAnotherVar = "Pascal"
Snake Case
Each word is separated by an underscore.
snake_case_var = "snake var"
Assigning Values to Multiple Variables
We can assign multiple variables in a single line.
a, b, c = "Var 1", "Var 2", "Var 3"
print(a)
print(b)
print(c)
One Value to Multiple Variables
We can assign the same value to multiple variables in a single line.
x = y = z = "multiple"
print(x)
print(y)
print(z)
Unpack a Collection
If we have a collection of values such as lists or tuples, we can extract the values into variables. This is called unpacking.
my_shopping_list = ["chips", "water", "rice"]
a, b, c = my_shopping_list
print(a)
print(b)
print(c)
Output Variables
We can use functions such as print() to output the value assigned to the variable.
x = "Output test"
print(x)
To output multiple values, we can separate them with a comma or use use the plus (+) operator.
x = "My "
y = "variable "
z = "here"
print(x, y, z)
print(x + y + z) #Without the space in the x and y variable value, it will be a single word.
When dealing with numbers, the plus (+) operator acts as a math operator. When attempting to use a variable with a number and string, we will be given an error.
x = 10
y = 20
z = "math"
print(x + y)
print(x + z)
To overcome this, the best way will be using commas (,) to output multiple variables as it works with different data types.
Global Variables
Global variables are variables created outside functions. Global variables can be used by everyone, both inside and outside functions.
var = "var1"
def myfunc():
print("This is" + x)
myfunc()
If the variable is created inside a function with the same name, it will be local and can only be used inside the function unless the global keyword has been used.
#No global keyword
x = "global var"
def myfunc():
x = "local var"
print("This is my " + x)
my func()
print ("This is my " + x)
#Using the global keyboard
def myfunc():
global y
y = "local var 2"
print("This is my " + y)
my func()
print("This is my " + y)